package com.ktjy.mapper.user;

import com.ktjy.pojo.SysUser;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;


public class SysUserMapperTest {
    private Logger logger = Logger.getLogger(SysUserMapperTest.class);
    @Test
    public void count() throws Exception{
        SqlSession sqlSession = null;
        try{
            String resource = "mybatis-config.xml";
            InputStream is = Resources.getResourceAsStream(resource);
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = factory.openSession();
            int count = sqlSession.getMapper(SysUserMapper.class).count();
            logger.debug("SysUserMapperTest count --->" +count);
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            sqlSession.close();
        }
    }
    @Test
    public void testUserListQuery() throws Exception{
        SqlSession sqlSession = null;
        List<SysUser> userList = null;
        try{
            String resource = "mybatis-config.xml";
            InputStream is =Resources.getResourceAsStream(resource);
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            sqlSession = factory.openSession();
            userList = sqlSession.getMapper(SysUserMapper.class).getUserList();
        }finally {
            sqlSession.close();
        }
        if(userList != null){
            for(SysUser user : userList){
                logger.debug("testUserListQuery account:  "+user.getAccount()+ " and realName:  "+user.getRealName());
            }
        }
    }
}
